package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.CollectionManager;
import cn.wolfcode.crm.exception.ClearException;
import cn.wolfcode.crm.exception.LessException;
import cn.wolfcode.crm.query.CollectionManagerQueryObject;
import cn.wolfcode.crm.service.ICollectionManagerService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;

@Controller
@RequestMapping("collectionManager")
public class CollectionManagerController {
    @Autowired
    public ICollectionManagerService collectionManagerService;

    @RequestMapping("view")
    @RequiresPermissions(value = {"collectionManager:view", "收款管理列表"}, logical = Logical.OR)
    public String view() {
        return "collectionManager/collectionManager";
    }

    @ResponseBody
    @RequestMapping("query")
    public PageResult query(CollectionManagerQueryObject qo) {
        PageResult query = collectionManagerService.query(qo);
        return query;
    }

    @ResponseBody
    @RequestMapping("saveOrUpdate")
    @RequiresPermissions(value = {"collectionManager:saveOrUpdate", "收款管理增加或编辑"}, logical = Logical.OR)
    public JSONResult saveOrUpdate(CollectionManager collectionManager) {
        try {
            if (collectionManager.getId() != null) {
                collectionManagerService.updateByPrimaryKey(collectionManager);
            } else {
                collectionManagerService.insert(collectionManager);
            }
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("操作失败");
        }
    }

    @RequestMapping("audit")
    @ResponseBody
    @RequiresPermissions(value = {"collectionManager:changeState", "收款审核"}, logical = Logical.OR)
    public JSONResult changeState(Long id, BigDecimal amount ,Long formalStudentId) {
        try {
            collectionManagerService.audit(id,amount,formalStudentId);
            return JSONResult.success("");
        }catch (LessException e){
            return JSONResult.mark(e.getMessage());
        }catch (ClearException e){
            return JSONResult.mark(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("操作失败");
        }
    }
}
